package com.dev.dream.exochatlockscreen.service;

import java.io.IOException;
import java.util.Random;

import android.app.KeyguardManager;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.os.IBinder;
import android.util.Log;

import com.dev.dream.exochatlockscreen.EXOChatKeypadPreferences;
import com.dev.dream.exochatlockscreen.blur.BlurBuilder;
import com.dev.dream.exochatlockscreen.receive.LockScreenReeiver;
import com.dev.dream.libs.adapter.ImageBitmap;
import com.dev.dream.libs.share.MyShare;

public class SLockscreen extends Service {
	BroadcastReceiver mReceiver;

	public static SLockscreen instance;
	public static boolean IS_RUN = false;

	public static int indexMark[];
	public static Bitmap background[];
//	public static BitmapDrawable backgroundBlur[];
	public static Bitmap backgroundIcon[];
	public static boolean loadBgIcon;

	public static Bitmap avatarIcon[];
	public static boolean loadAvatar;

	boolean isChangeBlurImage;

	
	public static Bitmap bitmapMyAvatar;
	public static boolean loadMyAvatar;
	
	public static int indexMsg;

	public static Typeface fontMsg;
	// Intent myIntent;
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {

		return Service.START_STICKY;
	}

	// private BitmapDrawable backgroundBlur;
	// public static HomeKeyLocker mHomeKeyLocker;
	int imgIndexBgBlur;

	@Override
	public void onCreate() {

		indexMark = new int[EXOChatKeypadPreferences.size_bg + 1];
		background = new Bitmap[EXOChatKeypadPreferences.size_bg + 1];
		//backgroundBlur = new BitmapDrawable[EXOChatKeypadPreferences.size_bg + 1];
		avatarIcon = new Bitmap[EXOChatKeypadPreferences.size_icon + 1];
		
		Random random = new Random();
		indexMsg = random.nextInt(40);
		fontMsg = Typeface.createFromAsset(getAssets(), "fonts/Arial.ttf");
		try {
			instance = this;

			isChangeBlurImage = true;

			EXOChatKeypadPreferences.getInstance().initPreferences(
					getApplicationContext());

			// imgIndexBgBlur = EXOChatKeypadPreferences.getInstance()
			// .getImgIndexBg();
			//
			// background[imgIndexBgBlur] = MyShare.getBitmapFromAssets(
			// getApplicationContext(), "bg/" + imgIndexBgBlur + ".jpg");
			//
			// backgroundBlur[imgIndexBgBlur] = new BitmapDrawable(
			// BlurBuilder.fastblur(background[imgIndexBgBlur], 20));
			// indexMark[imgIndexBgBlur] = 1;

			KeyguardManager.KeyguardLock k1;

			// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON|WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);

			KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
			k1 = km.newKeyguardLock("IN");
			k1.disableKeyguard();

			/*
			 * try{ StateListener phoneStateListener = new StateListener();
			 * TelephonyManager telephonyManager
			 * =(TelephonyManager)getSystemService(TELEPHONY_SERVICE);
			 * telephonyManager
			 * .listen(phoneStateListener,PhoneStateListener.LISTEN_CALL_STATE);
			 * }catch(Exception e){ System.out.println(e); }
			 */

			/*
			 * myIntent = new
			 * Intent(MyService.this,LockScreenAppActivity.class);
			 * myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); Bundle
			 * myKillerBundle = new Bundle(); myKillerBundle.putInt("kill",1);
			 * myIntent.putExtras(myKillerBundle);
			 */

			IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
			filter.addAction(Intent.ACTION_SCREEN_OFF);

			mReceiver = new LockScreenReeiver();
			registerReceiver(mReceiver, filter);

		} catch (Exception ex) {
		}
		super.onCreate();

	}

	public void changeBlurImage() {
		imgIndexBgBlur = EXOChatKeypadPreferences.getInstance().getImgIndexBg();
		if (indexMark[imgIndexBgBlur] == 0) {

			background[imgIndexBgBlur] = MyShare.getBitmapFromAssets(
					getApplicationContext(), "bg/" + imgIndexBgBlur + ".jpg");

//			backgroundBlur[imgIndexBgBlur] = new BitmapDrawable(
//					BlurBuilder.fastblur(background[imgIndexBgBlur], 20));
			indexMark[imgIndexBgBlur] = 1;

		}
	}

	@Override
	public void onStart(Intent intent, int startId) {
		// TODO Auto-generated method stub

		super.onStart(intent, startId);
	}

	/*
	 * class StateListener extends PhoneStateListener{
	 * 
	 * @Override public void onCallStateChanged(int state, String
	 * incomingNumber) {
	 * 
	 * super.onCallStateChanged(state, incomingNumber); switch(state){ case
	 * TelephonyManager.CALL_STATE_RINGING: break; case
	 * TelephonyManager.CALL_STATE_OFFHOOK:
	 * System.out.println("call Activity off hook");
	 * getApplication().startActivity(myIntent);
	 * 
	 * 
	 * 
	 * break; case TelephonyManager.CALL_STATE_IDLE: break; } } };
	 */

	@Override
	public void onDestroy() {
		unregisterReceiver(mReceiver);
		instance = null;
		Log.d("Kai", "Service destroy");
		super.onDestroy();
	}

	public boolean isChangeBlurImage() {
		return isChangeBlurImage;
	}

	public void setChangeBlurImage(boolean isChangeBlurImage) {
		this.isChangeBlurImage = isChangeBlurImage;
	}

	public void loadBgBitmapIcon() {
		backgroundIcon = new Bitmap[EXOChatKeypadPreferences.size_bg + 1];
		for (int i = 1; i <= EXOChatKeypadPreferences.size_bg; i++) {
			backgroundIcon[i] = getBitmap("bg/" + i + ".jpg", 200, 355);
		}
	}

	public void loadAvatarIcon() {

		for (int i = 1; i <= EXOChatKeypadPreferences.getInstance().size_icon; i++) {

			avatarIcon[i] = getBitmapFromAssets("ic/" + i + ".jpg");
		}
	}

	public Bitmap getBitmapFromAssets(String fileName) {

		Bitmap bitmap = null;

		try {
			bitmap = BitmapFactory.decodeStream(getAssets().open(fileName));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;
	}

	public Bitmap getBitmap(String fileName, int width, int height) {
		Bitmap bitmap = null;

		try {
			bitmap = getResizedBitmap(
					BitmapFactory.decodeStream(getAssets().open(fileName)),
					width, height);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return bitmap;

	}

	public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
		int width = bm.getWidth();
		int height = bm.getHeight();
		float scaleWidth = ((float) newWidth) / width;
		float scaleHeight = ((float) newHeight) / height;
		// CREATE A MATRIX FOR THE MANIPULATION
		Matrix matrix = new Matrix();
		// RESIZE THE BIT MAP
		matrix.postScale(scaleWidth, scaleHeight);

		// "RECREATE" THE NEW BITMAP
		Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height,
				matrix, false);
		return resizedBitmap;
	}

	public void loadMyAvatar() {
		BitmapFactory.Options options = new BitmapFactory.Options();
		options.inPreferredConfig = Bitmap.Config.ARGB_8888;
		bitmapMyAvatar = BitmapFactory.decodeFile(EXOChatKeypadPreferences
				.getInstance().getUrlMyAvatar(), options);
	}

}
